import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

import '../state/base_state.dart';
import '../view_model/base_list_view_model.dart';

abstract class BaseView {
  /// 是否保持页面活跃
  bool isAutoKeepAlive = false;

  /// context
  BuildContext? get context => _state.context;
 
  /// state
  late BaseState _state;

  BaseState get state => _state;

  /// 注册state
  void registerState(BaseState state) {
    _state = state;
    _state.isAutoKeepAlive = isAutoKeepAlive;

    /// 页面是否保活
  }

  /// 刷新页面的操作 封装setState之后取名相同
  void setState(VoidCallback fn) {
    _state.refreshState(fn);
  }

  /// page或者widget的状态是否初始化 或者 销毁
  bool isActive = false;

  /// 生命周期
  /// 暂时只需要这么几个 其他有需要自己添加
  /// */
  @mustCallSuper
  void initState() {
    isActive = true;
  }

  @protected
  @mustCallSuper
  void didChangeDependencies() {}

  @mustCallSuper
  Widget build(BuildContext context);

  @mustCallSuper
  void reassemble() {}

  @mustCallSuper
  void dispose() {
    isActive = true;
  }

  /// 初始化获取到context
  void initStateForContext(BuildContext context) {}

  Widget get spacer => const Spacer();

  Widget gapX(double width) => SizedBox(
        width: width,
      );

  Widget gapY(double height) => SizedBox(
        height: height,
      );

  void logd(String msg) => debugPrint(msg);
}

/// 给ListView/GridView 空状态使用
Widget emptyWidgetForScrollView<T extends BaseListViewModel>(
    BuildContext context,
    {double? height,
    double? width,
    String? showText}) {
  return RefreshIndicator(
    color: Colors.blue,
    backgroundColor: Colors.white,
    onRefresh: () async => context.read<T>().onRefresh(),
    child: ListView(
      children: [
        SizedBox(height: 60.h),
        SizedBox(
          width: 185.h,
          height: 185.h,
          child: Image.asset(
            'assets/images/image/no_data.png',
            fit: BoxFit.cover,
          ),
        ),
        Center(
          child: Text(
            showText ?? "无数据",
            style: TextStyle(
                fontSize: 16.sp,
                color: Colors.black,
                fontWeight: FontWeight.w500),
          ),
        ),
      ],
    ),
  );
}
